/**
 * Copyright &copy; 2015-2020 <a href="http://www.jeeplus.org/">JeePlus</a> All rights reserved.
 */
package com.jeeplus.modules.sys.web;

import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.exceptions.ClientException;
import com.jeeplus.common.json.AjaxJson;
import com.jeeplus.common.utils.MyBeanUtils;
import com.jeeplus.common.utils.sms.SmsUtils;
import com.jeeplus.common.utils.sms.SmsUtilsByTencent;
import com.jeeplus.core.web.BaseController;
import com.jeeplus.modules.sys.entity.SysConfig;
import com.jeeplus.modules.sys.service.SysConfigService;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.sms.v20190711.models.SendStatus;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 系统配置Controller
 * @author 刘高峰
 * @version 2018-10-18
 */
@RestController
@RequestMapping("/sys/sysConfig")
public class SysConfigController extends BaseController {

	@Autowired
	private SysConfigService sysConfigService;


	/**
	 * 系统配置列表页面
	 */
	@GetMapping("queryById")
	public AjaxJson queryById(HttpServletRequest request, HttpServletResponse response, Model model) {
		SysConfig config = sysConfigService.get("1");
		return AjaxJson.success().put("config", config);
	}
	/**
	 * 系统配置列表页面
	 */
	@GetMapping("queryByIdIndex")
	public AjaxJson queryByIdIndex(HttpServletRequest request, HttpServletResponse response, Model model) {
		SysConfig config = sysConfigService.get("1");
		return AjaxJson.success().put("logo", config.getLogo()).put("productName",config.getProductName());
	}

	@GetMapping("testSms")
	public AjaxJson testSms(@RequestParam("tel")String tel)throws ClientException {
		SendSmsResponse response =  SmsUtils.sendSms(tel,"{code:123}");
		if (response.getCode() != null && response.getCode().equals("OK")) {
			return AjaxJson.success("短信发送成功!");
		}else {
			return AjaxJson.error("短信发送失败!"+ response.getMessage());
		}
	}
	//@ApiOperation(value = "发送注册验证码")
	@ApiImplicitParams({
			@ApiImplicitParam(value = "手机号", name = "tel", required = true, paramType = "query", dataType = "string"),
			@ApiImplicitParam(value = "注册验证码(6位数)", name = "verificationCode", required = true, paramType = "query", dataType = "string")})
	@GetMapping("sendSmsVerificationCode")
	public AjaxJson sendSmsVerificationCode(@RequestParam("tel")String tel, @RequestParam("verificationCode")String verificationCode) throws TencentCloudSDKException {
		SendStatus sendStatus = SmsUtilsByTencent.sendSmsVerificationCode(tel,verificationCode);
		if (sendStatus.getCode() != null && sendStatus.getCode().equals("Ok")) {
			return AjaxJson.success("短信发送成功!");
		}else {
			return AjaxJson.error("短信发送失败!"+ sendStatus.getMessage());
		}
	}

	/**
	 * 保存系统配置
	 */
	@PostMapping("save")
	public AjaxJson save(SysConfig config)throws Exception {
		AjaxJson j = new AjaxJson();
		if(jeePlusProperites.isDemoMode()){
			return AjaxJson.error("演示模式，禁止修改!");
		}
		if(config.getMultiAccountLogin() == null){
			config.setMultiAccountLogin("0");
		}
		SysConfig target = sysConfigService.get("1");
		MyBeanUtils.copyBeanNotNull2Bean(config, target);
		target.setIsNewRecord(false);
		sysConfigService.save(target);
		return AjaxJson.success("保存系统配置成功").put("config", target);
	}

}
